from typing import *
from collections import deque


class Solution:

    def calcEquation(self, equations: List[List[str]], values: List[float],
                     queries: List[List[str]]) -> List[float]:
        h = dict()
        nexts = DefaultDict(set)
        for (a, b), v in zip(equations, values):
            nexts[a].add(b)
            h[(a, b)] = v
            nexts[b].add(a)
            h[(b, a)] = 1 / v

        ans = []
        for a, b in queries:
            if a not in nexts or b not in nexts:
                ans.append(-1)
            else:
                visit = set()
                visit.add(a)
                q = deque([(a,1)])
                cur = -1
                while q:
                    p,v = q.pop()
                    if p == b:
                        cur = v
                        break
                    for next in nexts[p]:
                        if next not in visit:
                            visit.add(next)
                            q.append((next,v*h[(p,next)]))
                ans.append(cur)


        return ans


s = Solution()
print(
    s.calcEquation(equations=[["a", "b"], ["b", "c"]],
                   values=[2.0, 3.0],
                   queries=[["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"],
                            ["x", "x"]]))
